package com.asl.website.advantage.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.asl.website.advantage.domain.AdvantageContentDTO;
import com.asl.website.serviceContent.domain.SysServiceContent;
import com.asl.website.serviceContent.domain.SysServiceContentDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.asl.common.annotation.Log;
import com.asl.common.core.controller.BaseController;
import com.asl.common.core.domain.AjaxResult;
import com.asl.common.enums.BusinessType;
import com.asl.website.advantage.domain.SysAdvantage;
import com.asl.website.advantage.service.ISysAdvantageService;
import com.asl.common.utils.poi.ExcelUtil;
import com.asl.common.core.page.TableDataInfo;

/**
 * 优势Controller
 * 
 * @author ruoyi
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/advantage/advantage")
public class SysAdvantageController extends BaseController
{
    @Autowired
    private ISysAdvantageService sysAdvantageService;
    /**
     * 获取所有优势列表（按排序）
     */
    @GetMapping("/content/{typeId}")
    public AjaxResult getAllContents(
            @PathVariable("typeId") Integer typeId,
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage) {
        SysAdvantage query = new SysAdvantage();
        query.setTypeId(typeId);
        List<SysAdvantage> contents = sysAdvantageService.selectSysAdvantageList(query);
        if (contents.isEmpty()) {
            return AjaxResult.error("未配置任何服务内容");
        }

        List<AdvantageContentDTO> result = contents.stream()
                .map(content -> new AdvantageContentDTO(content, acceptLanguage))
                .sorted(Comparator.comparingInt(AdvantageContentDTO::getSortOrder))
                .collect(Collectors.toList());

        return AjaxResult.success(result);
    }
    /**
     * 查询优势列表
     */
    @PreAuthorize("@ss.hasPermi('advantage:advantage:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAdvantage sysAdvantage)
    {
        startPage();
        List<SysAdvantage> list = sysAdvantageService.selectSysAdvantageList(sysAdvantage);
        return getDataTable(list);
    }

    /**
     * 导出优势列表
     */
    @PreAuthorize("@ss.hasPermi('advantage:advantage:export')")
    @Log(title = "优势", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAdvantage sysAdvantage)
    {
        List<SysAdvantage> list = sysAdvantageService.selectSysAdvantageList(sysAdvantage);
        ExcelUtil<SysAdvantage> util = new ExcelUtil<SysAdvantage>(SysAdvantage.class);
        util.exportExcel(response, list, "优势数据");
    }

    /**
     * 获取优势详细信息
     */
    @PreAuthorize("@ss.hasPermi('advantage:advantage:query')")
    @GetMapping(value = "/{advantageId}")
    public AjaxResult getInfo(@PathVariable("advantageId") Long advantageId)
    {
        return success(sysAdvantageService.selectSysAdvantageByAdvantageId(advantageId));
    }

    /**
     * 新增优势
     */
    @PreAuthorize("@ss.hasPermi('advantage:advantage:add')")
    @Log(title = "优势", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAdvantage sysAdvantage)
    {
        return toAjax(sysAdvantageService.insertSysAdvantage(sysAdvantage));
    }

    /**
     * 修改优势
     */
    @PreAuthorize("@ss.hasPermi('advantage:advantage:edit')")
    @Log(title = "优势", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAdvantage sysAdvantage)
    {
        return toAjax(sysAdvantageService.updateSysAdvantage(sysAdvantage));
    }

    /**
     * 删除优势
     */
    @PreAuthorize("@ss.hasPermi('advantage:advantage:remove')")
    @Log(title = "优势", businessType = BusinessType.DELETE)
	@DeleteMapping("/{advantageIds}")
    public AjaxResult remove(@PathVariable Long[] advantageIds)
    {
        return toAjax(sysAdvantageService.deleteSysAdvantageByAdvantageIds(advantageIds));
    }
}
